package com.link.play.fit.ai.ui.guide


import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.R
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.FgChooseWeightBinding
import com.link.play.fit.ai.ui.guide.ac.GuidePbAc
import com.link.play.fit.ai.ui.setting.SetUserInfoAc
import com.link.play.fit.ai.ui.weightcontrol.ac.WeighControlPbAc
import com.link.play.fit.ai.utils.convertKgToLbs
import com.link.play.fit.ai.utils.convertLbsToKg
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import javax.annotation.meta.When

class ChooseWeightFg : BasePageFragment<FgChooseWeightBinding>(FgChooseWeightBinding::inflate) {


    lateinit var userGuideState: MutableStateFlow<UserGuide>
    var rulerViewCallBack: ((vale: Int) -> Unit)? = null

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        when (val ac = requireActivity()) {
            is GuidePbAc -> {// 引导页面
                userGuideState = ac.viewModel.userGuideState
            }

            is SetUserInfoAc -> {//设置界面的要用
                userGuideState = ac.viewModel.userGuideState
                binding.tvTitle.visibility = View.GONE
            }

            is WeighControlPbAc -> {
                userGuideState = ac.viewModel.userGuideState
            }
        }

        lifecycleScope.launch {

            userGuideState.collectLatest {
                when (userGuideState.value.goal) {
                    0 -> {
                        binding.tvGoal.text = getString(R.string.gain_weight)
                    }

                    1 -> {
                        binding.tvGoal.text = getString(R.string.maintain_weight)
                    }

                    2 -> {
                        binding.tvGoal.text = getString(R.string.lose_weight)
                    }
                }

                changeUI(it)
                changeRulerValue(it)
            }
        }


        binding.rulerView.setOnValueChangedListener { value ->
            val data = userGuideState.value
            if (data.imperialMetric) {
                data.goalWeightKg = value.toInt()
                data.goalWeightLbs = convertKgToLbs(value.toInt())
            } else {
                data.goalWeightLbs = value.toInt()
                data.goalWeightKg = convertLbsToKg(value.toInt())
            }
            changeUI(data)

            rulerViewCallBack?.invoke(value.toInt())
            rulerViewCallBack = null
        }

    }


    private fun changeRulerValue(it: UserGuide) {
        if (it.imperialMetric) {
            if (binding.rulerView.maxValue != 360f) {
                if (it.goalWeightKg >= 20f) {
                    binding.rulerView.setInitialValue(it.weightKg.toFloat())
                    binding.rulerView.setValue(20f, 360f, it.goalWeightKg.toFloat(), 1f, 10)
                }
            } else {
                binding.rulerView.currentValue = it.goalWeightKg.toFloat()
            }
        } else {
            if (binding.rulerView.maxValue != 800f) {
                if (it.goalWeightLbs >= 40f) {
                    binding.rulerView.setInitialValue(it.weightLbs.toFloat())
                    binding.rulerView.setValue(40f, 800f, it.goalWeightLbs.toFloat(), 1f, 10)
                }
            } else {
                binding.rulerView.currentValue = it.goalWeightLbs.toFloat()
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun changeUI(it: UserGuide) {
        if (it.imperialMetric) {
            if (it.goalWeightKg > it.weightKg) {
                binding.tvGoal.text = resources.getString(R.string.gain_weight)
            } else if (it.goalWeightKg < it.weightKg) {
                binding.tvGoal.text = resources.getString(R.string.lose_weight)
            } else {
                binding.tvGoal.text = resources.getString(R.string.maintain_weight)
            }
            binding.tvWeight.text = "${it.goalWeightKg} kg"
        } else {
            if (it.goalWeightLbs > it.weightLbs) {
                binding.tvGoal.text = resources.getString(R.string.gain_weight)
            } else if (it.goalWeightLbs < it.weightLbs) {
                binding.tvGoal.text = resources.getString(R.string.lose_weight)
            } else {
                binding.tvGoal.text = resources.getString(R.string.maintain_weight)
            }
            binding.tvWeight.text = "${it.goalWeightLbs} lbs"
        }

    }
}